package com.free.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.free.dao.Assert;
import com.free.dao.UserDao;
import com.free.entity.UserEntity;
import com.free.service.UserService;
import com.free.utils.RRException;

/**
 * 
* @ClassName: UserServiceImpl 
* @Description: TODO(用户service的实现类) 
* @author wujie
* @email 602991693@qq.com 
* @date 2017年8月31日 上午10:22:52 
*
 */
@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	@Override
	public UserEntity queryObject(Long userId) {
		
		return userDao.queryObject(userId);
	}

	@Override
	public List<UserEntity> queryList(Map<String, Object> map) {
		
		return userDao.queryList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		
		return userDao.queryTotal(map);
	}

	@Override
	public void save(String mobile, String password) {
		UserEntity user = new UserEntity();
		user.setMobile(mobile);
		user.setUsername(mobile);
		user.setPassword(DigestUtils.sha256Hex(password));
		user.setCreateTime(new Date());
		userDao.save(user);
		
	}

	@Override
	public void update(UserEntity user) {
		userDao.update(user);
		
	}

	@Override
	public void delete(Long userId) {
		userDao.delete(userId);
		
	}

	@Override
	public void deleteBatch(Long[] userId) {
		userDao.deleteBatch(userId);
		
	}

	@Override
	public UserEntity queryByMobile(String mobile) {
		
		return userDao.queryByMobile(mobile);
	}

	@Override
	public long login(String mobile, String password) {
		UserEntity user = queryByMobile(mobile);
		Assert.isNull(user, "手机号或密码错误");
		if(!user.getPassword().equals(DigestUtils.sha256Hex(password))){
			throw new RRException("手机号或密码错误");
		}
		return user.getUserId();
	}
	
}
